#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

int g_tickets =100;
pthread_mutex_t lock;

void* pthread_start(void*arg){
    while(1){
    pthread_mutex_lock(&lock);
        if(g_tickets<=0){
            pthread_mutex_unlock(&lock);
            break;
        }
        printf("i am pthred:%p  i am g_tickets %d\n",pthread_self(),g_tickets);

        g_tickets--;
        pthread_mutex_unlock(&lock);
        usleep(100);
    }
}


int main(){
    pthread_mutex_init(&lock,NULL);
    pthread_t tid[2];
    int i=0;
    for(i;i<2;i++){
        int ret =pthread_create(&tid[i],NULL,pthread_start,NULL);
        if(ret<0){
            perror("pthread_create");
            return 0;
        }
    }
    i=0;
    for(i;i<2;i++){
        pthread_join(tid[i],NULL);  
    }
    pthread_mutex_destroy(&lock);
    return 0;
}
